package org.jeecg.modules.website.service.impl;

import org.jeecg.modules.website.entity.WebWorkDynamic;
import org.jeecg.modules.website.entity.WebWorkFile;
import org.jeecg.modules.website.mapper.WebWorkDynamicMapper;
import org.jeecg.modules.website.mapper.WebWorkFileMapper;
import org.jeecg.modules.website.service.IWebWorkDynamicService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import org.springframework.transaction.annotation.Transactional;

import java.io.Serializable;
import java.util.Collection;
import java.util.List;

/**
 * @Description: 工作动态表
 * @Author: jeecg-boot
 * @Date:   2024-07-04
 * @Version: V1.0
 */
@Service
public class WebWorkDynamicServiceImpl extends ServiceImpl<WebWorkDynamicMapper, WebWorkDynamic> implements IWebWorkDynamicService {

    @Autowired
    private WebWorkDynamicMapper webWorkDynamicMapper;
    @Autowired
    private WebWorkFileMapper webWorkFileMapper;
    @Override
    @Transactional(rollbackFor = Exception.class)
    public void saveMain(WebWorkDynamic webWorkDynamic, List<WebWorkFile> webWorkFileList) {
        webWorkDynamicMapper.insert(webWorkDynamic);
        if(webWorkFileList!=null && webWorkFileList.size()>0) {
            for(WebWorkFile entity:webWorkFileList) {
                //外键设置
                entity.setGuideId(webWorkDynamic.getId());
                webWorkFileMapper.insert(entity);
            }
        }
    }

    @Override
    @Transactional(rollbackFor = Exception.class)
    public void updateMain(WebWorkDynamic webWorkDynamic,List<WebWorkFile> webWorkFileList) {
        webWorkDynamicMapper.updateById(webWorkDynamic);

        //1.先删除子表数据
        webWorkFileMapper.deleteByMainId(webWorkDynamic.getId());

        //2.子表数据重新插入
        if(webWorkFileList!=null && webWorkFileList.size()>0) {
            for(WebWorkFile entity:webWorkFileList) {
                //外键设置
                entity.setGuideId(webWorkDynamic.getId());
                webWorkFileMapper.insert(entity);
            }
        }
    }

    @Override
    @Transactional(rollbackFor = Exception.class)
    public void delMain(String id) {
        webWorkFileMapper.deleteByMainId(id);
        webWorkDynamicMapper.deleteById(id);
    }

    @Override
    @Transactional(rollbackFor = Exception.class)
    public void delBatchMain(Collection<? extends Serializable> idList) {
        for(Serializable id:idList) {
            webWorkFileMapper.deleteByMainId(id.toString());
            webWorkDynamicMapper.deleteById(id);
        }
    }



}
